package subnarines;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class World extends JPanel {
    public static int WIDTH = 641;
    public static int HEIGHT = 479;

    public static final int RUNNING = 0;
    public static final int PAUSE = 1;
    public static final int GAME_OVER = 2;
    private int state = RUNNING;

    private BattleShip ship = new BattleShip();
    private SeaObject submarines[] = {};
    private Mine mines[] = {};
    private Bomb bombs[] = {};
    private Ball balls[] = {};
    private Resupply resupplies[] = {};

    private SeaObject nextObject(){
        Random ran = new Random();
        int type = ran.nextInt(41);
        if(type<10){
            return new ObserveSubmarine();
        }else if(type<20){
            return new TropedoSubmarine();
        }else if(type<28){
            return new MineSubmarine();
        }else if(type<35){
            return new CxkSubmarine();
        }else{
            return new Plane();
        }
    }  //下一个潜艇或飞机

    private int subEnterIndex;
    private void submarineEnterAction(){
        subEnterIndex++;
        if(subEnterIndex%40==0){
            SeaObject obj = nextObject();
            submarines = Arrays.copyOf(submarines,submarines.length+1);
            submarines[submarines.length-1] = obj;
        }
    }//潜艇和飞机入场


    private int mineEnterIndex;
    private void mineEnterAction(){
        mineEnterIndex++;
        if(mineEnterIndex%100==0){
            for (int i = 0; i < submarines.length; i++) {
                if(submarines[i] instanceof MineSubmarine){
                    MineSubmarine ms = (MineSubmarine)submarines[i];
                    Mine obj =ms.shootMine();
                    mines = Arrays.copyOf(mines,mines.length+1);
                    mines[mines.length-1] = obj;
                }
            }
        }
    }     //水雷入场

    private int ballEnterIndex;
    private void ballEnterAction(){
        ballEnterIndex++;
        if(ballEnterIndex%100==0){
            for (int i = 0; i < submarines.length; i++) {
                if(submarines[i] instanceof  CxkSubmarine){
                    CxkSubmarine cs = (CxkSubmarine)submarines[i];
                    Ball obj = cs.shootBall();
                    balls = Arrays.copyOf(balls,balls.length+1);
                    balls[balls.length-1] = obj;
                }
            }
        }
    }     //篮球入场

    private int resupplyEnterIndex;
    private void resupplyEnterAction(){
        resupplyEnterIndex++;
        if(resupplyEnterIndex%100==0){
            for (int i = 0; i < submarines.length; i++) {
                if(submarines[i] instanceof  Plane){
                    Plane pl = (Plane)submarines[i];
                    Resupply obj = pl.throwResupply();
                    resupplies = Arrays.copyOf(resupplies,resupplies.length+1);
                    resupplies[resupplies.length-1] = obj;
                }
            }
        }
    }//武器箱入场

    private void moveAction(){             //移动
        for (int i = 0; i < submarines.length; i++) {
            submarines[i].move();
        }
        for (int i = 0; i < mines.length; i++) {
            mines[i].move();
        }
        for (int i = 0; i < bombs.length; i++) {
            bombs[i].move();
        }
        for (int i = 0; i < balls.length; i++) {
            balls[i].move();
        }
        for (int i = 0; i < resupplies.length; i++) {
            resupplies[i].move();
        }
    }          //移动

    private void outOfBoundsAction() {
        for (int i = 0; i < submarines.length; i++) {
            if (submarines[i].isOutOfBounds()) {
                submarines[i] = submarines[submarines.length - 1];
                submarines = Arrays.copyOf(submarines, submarines.length - 1);
            }
        }
        for (int i = 0; i < mines.length; i++) {
            if (mines[i].isOutOfBounds()) {
                mines[i] = mines[mines.length - 1];
                mines = Arrays.copyOf(mines, mines.length - 1);
            }
        }
        for (int i = 0; i < bombs.length; i++) {
            if (bombs[i].isOutOfBounds()) {
                bombs[i] = bombs[bombs.length - 1];
                bombs = Arrays.copyOf(bombs, bombs.length - 1);
            }
        }
        for (int i = 0; i < balls.length; i++) {
            if(balls[i].isOutOfBounds()){
                balls[i] = balls[balls.length-1];
                balls = Arrays.copyOf(balls,balls.length-1);
            }
        }
        for (int i = 0; i < resupplies.length; i++) {
            if(resupplies[i].isOutOfBounds()){
                resupplies[i] = resupplies[resupplies.length-1];
                resupplies = Arrays.copyOf(resupplies,resupplies.length-1);
            }
        }
    }  //越界

    private int score = 0;
    private void bombBangAction(){
        for (int i = 0; i < bombs.length; i++) {
            Bomb b = bombs[i];
            for (int j = 0; j < submarines.length; j++) {
                SeaObject s = submarines[j];
                if(b.isLive() && s.isLive() && s.isHit(b)){
                    s.goDead();
                    b.goDead();
                    if(s instanceof EnemyScore){
                        EnemyScore es = (EnemyScore)s;
                        score += es.getScore();
                    }
                    if(s instanceof EnemyLife){
                        EnemyLife el = (EnemyLife)s;
                        int num = el.getLife();
                        ship.addLife(num);
                    }
                }
            }
        }
    }      //炸弹爆炸

    private void mineBangAction(){
        for (int i = 0; i < mines.length; i++) {
            Mine m = mines[i];
            if(m.isLive() && ship.isLive() && m.isHit(ship)){
                m.goDead();
                ship.subtractLife();
            }
        }
    }      //水雷爆炸

    private void ballBangAction(){
        for (int i = 0; i < balls.length; i++) {
            Ball b = balls[i];
            if(b.isLive() && ship.isLive() && b.isHit(ship)){
                b.goDead();
                score -= ship.subtractScore();
            }
        }
    }      //篮球爆炸

    private void resupplyBangAction(){
        for (int i = 0; i < resupplies.length; i++) {
            Resupply r = resupplies[i];
            if(r.isLive() && ship.isLive() && r.isHit(ship)){
                r.goDead();
                ship.payload += 10;
            }
        }
    }  //接收弹药箱

    private void checkGameOverAction(){
        if(ship.getLife() <= 0){
            state = GAME_OVER;
        }
    } //检测游戏结束

    private void action(){
        KeyAdapter k = new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_P){
                    if(state == RUNNING){
                        state = PAUSE;
                    }else if (state == PAUSE){
                        state = RUNNING;
                    }
                }
                if(state==RUNNING){
                    if(ship.payload > 0){
                        if(e.getKeyCode() == KeyEvent.VK_SPACE){
                            Bomb obj = ship.shootBomb();
                            bombs = Arrays.copyOf(bombs,bombs.length+1);
                            bombs[bombs.length-1] = obj;
                        }
                    }
                    if(e.getKeyCode() == KeyEvent.VK_LEFT){
                        ship.moveLeft();
                    }
                    if(e.getKeyCode() == KeyEvent.VK_RIGHT){
                        ship.moveRight();
                    }
                }
            }
        };
        this.addKeyListener(k);

        java.util.Timer timer = new Timer();  //定时器
        int interval = 10;
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if(state==RUNNING){
                    submarineEnterAction();
                    mineEnterAction();
                    ballEnterAction();
                    resupplyEnterAction();
                    moveAction();
                    outOfBoundsAction();
                    bombBangAction();
                    mineBangAction();
                    ballBangAction();
                    resupplyBangAction();
                    checkGameOverAction();
                    repaint();
                }
            }
        }, interval, interval);
    }

    public void paint(Graphics g){
        Images.sea.paintIcon(null,g,0,0);
        ship.paintImage(g);
        for (int i = 0; i < submarines.length; i++) {
            submarines[i].paintImage(g);
        }
        for (int i = 0; i < mines.length; i++) {
            mines[i].paintImage(g);
        }
        for (int i = 0; i < bombs.length; i++) {
            bombs[i].paintImage(g);
        }
        for (int i = 0; i < balls.length; i++) {
            balls[i].paintImage(g);
        }
        for (int i = 0; i < resupplies.length; i++) {
            resupplies[i].paintImage(g);
        }
        g.drawString("SCORE"+score,150,50);
        g.drawString("LIFE"+ship.getLife(),300,50);
        g.drawString("PAYLOAD"+ship.payload,500,50);
        if(state == GAME_OVER){
            Images.gameover.paintIcon(null,g,0,0);
        }
    }        //画图


    public static void main(String[] args) {

        JFrame frame = new JFrame();
        World world = new World();
        world.setFocusable(true);
        frame.add(world);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(WIDTH+6,HEIGHT+28);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);

        world.action();
    }
}
